package com.sugon.controller.analyse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.entity.ModuleTypeEntity;
import com.sugon.modules.analyse.service.IMoldCostAnalyseService;
import com.sugon.service.ModuleTypeService;
import com.sugon.utils.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @description: 模具成本统计分析Controller
 * @author: YuXD
 * @create: 2023-02-27
 **/
@RestController
public class MoldCostAnalyseController {

    @Resource
    private ModuleTypeService moldTypeService;
    @Resource
    private IMoldCostAnalyseService moldCostAnalyseService;

    /**
     * 获取模具类型成本统计
     * 对每个模具类型进行成本分析
     *
     * @param yearly 统计年份
     * @return
     */
    @GetMapping("/moldType/cost")
    public R loadMoldSingleCostStat(@RequestParam(required = false) String yearly) {
        // 如果统计年份未指定，默认为今年
        if (StrUtil.isEmpty(yearly)) {
            yearly = DateUtil.thisYear() + "";
        }
        return R.ok().put("statResult", moldCostAnalyseService.doMoldTypeCostStat(yearly, null));
    }

    /**
     * 获取模具类型关联的各位置备件明细统计
     *
     * @param moldTypeId 模具类型ID
     * @param yearly     年度
     * @return
     */
    @GetMapping("/moldType/position/cost")
    public R loadMoldTypePositionCostStat(@RequestParam String moldTypeId, @RequestParam(required = false) String yearly) {
        // 校验模具类型是否存在
        ModuleTypeEntity moldType = moldTypeService.queryObject(moldTypeId);
        if (moldType == null) {
            return R.error("模具类型不存在！");
        }
        if (StrUtil.isEmpty(yearly)) {
            yearly = DateUtil.thisYear() + "";
        }
        // 获取所有模具状态信息
        return R.ok().put("contrastResult", moldCostAnalyseService.getPositionCostStatItem(moldTypeId, yearly));
    }

    /**
     * 获取模具单台成本统计
     *
     * @param yearly     统计年份
     * @param moduleType 模具类型ID
     * @return
     */
    @GetMapping("/mold/cost")
    public R loadMoldSingleCostStat(@RequestParam(required = false) String yearly, @RequestParam(required = false) String moduleType) {
        // 如果统计年份未指定，默认为今年
        if (StrUtil.isEmpty(yearly)) {
            yearly = DateUtil.thisYear() + "";
        }
        return R.ok().put("statResult", moldCostAnalyseService.doMoldCostStat(yearly, moduleType));
    }

    /**
     * 获取模具单台成本折线图统计
     *
     * @param yearly     年度
     * @param moduleType 模具类型ID
     * @return
     */
    @GetMapping("/mold/cost/line")
    public R loadMoldSingleCostChartsStat(@RequestParam(required = false) String yearly, @RequestParam(required = false) String moduleType) {
        // 如果统计年份未指定，默认为今年
        if (StrUtil.isEmpty(yearly)) {
            yearly = DateUtil.thisYear() + "";
        }
        return R.ok().put("statResult", moldCostAnalyseService.doMoldCostLineStat(yearly, moduleType));
    }

}
